package command.project.component;

import javax.xml.bind.annotation.XmlTransient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import controller.ApplicationController;
import model.StatechartComponent;
import model.StatechartComponentManager;
import model.util.Point;

public class MoveComponentCommand extends AbstractComponentCommand {

	@XmlTransient
	private static final Logger log = LoggerFactory
			.getLogger(MoveComponentCommand.class);

	private Long projectID;
	private Long componentId;
	private Point newLocation;

	public MoveComponentCommand(Long projectID, StatechartComponent component,
			Point newLocation) {
		super();
		this.projectID = projectID;
		this.componentId = component.getId();
		this.newLocation = newLocation;
	}

	@Override
	public void executeLocal(ApplicationController applicationController) {
		log.debug("Executing command : MoveComponent ...");
		StatechartComponentManager scm = applicationController
				.getProjectController().getStatechartComponentManager();
		StatechartComponent component = scm.get(componentId);
		if (component != null) {
			component.setLocation(newLocation);
			log.debug("Executing command : MoveComponent DONE");
		} else {
			log.error("Executing command : MoveComponent FAILED (component is null)");
		}
	}

	@Override
	public void executeRemote(ApplicationController applicationController) {
		log.debug("Executing remote command : MoveComponent ...");
		if (applicationController.getModel().getCurrentProject() != null
				&& applicationController.getModel().getCurrentProject().getId()
						.equals(projectID)) {
			log.debug("Executing remote command : MoveComponent (projet is open) ...");
			StatechartComponentManager scm = applicationController
					.getProjectController().getStatechartComponentManager();
			StatechartComponent component = scm.get(componentId);
			if (component != null) {
				component.setLocation(newLocation);
				log.debug("Executing remote command : MoveComponent moving ...");
			}
		} else {
			log.debug("Executing remote command : MoveComponent (projet is not open) ...");
		}
		log.debug("Executing remote command : MoveComponent DONE");

	}

}
